package com.jintoufs.web.action.sys;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jintoufs.dao.QueryFilters;
import com.jintoufs.domain.sys.Dictionary;
import com.jintoufs.logAnnotation.SystemControllerLog;
import com.jintoufs.service.sys.DictionaryService;
import com.jintoufs.web.action.support.JqgridPage;
import com.jintoufs.web.action.support.SupportAction;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author zhaokai
 * @Description: 系统字典
 * @date 2016年2月19日 下午4:26:36
 */
@Controller
@Scope("prototype")
@RequestMapping("/dic")
public class DictionaryAction extends SupportAction {

    @Resource
    private DictionaryService dictionaryService;

    @RequestMapping("/list")
    public String list() {
        return "dictionary/list";
    }

    @RequestMapping("/oper")
    @SystemControllerLog(description = "系统字典")
    public void oper(HttpServletResponse response, String oper, Dictionary dictionary) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        if (oper.equals("del")) {
            dictionaryService.delete(dictionary.getId());
        } else if (oper.equals("edit")) {
            dictionaryService.update(dictionary);
            ajaxMsg.setCode("200");
            ajaxMsg.setMessage("更新成功");
        } else if (oper.equals("add")) {
            Dictionary tmp = dictionaryService.insert(dictionary);
            if (tmp == null) {
                ajaxMsg.setCode("500");
                ajaxMsg.setMessage("添加失败");
            } else {
                ajaxMsg.setCode("200");
                ajaxMsg.setMessage("添加成功");
            }
        } else {
            ajaxMsg.setCode("500");
            ajaxMsg.setMessage("操作代码错误！系统只提供增加/修改/删除功能。");
            writeInfo(response, JSON.toJSONString(ajaxMsg));
        }
        writeInfo(response, JSON.toJSONString(ajaxMsg));
    }

    @RequestMapping("/delete")
    @SystemControllerLog(description = "获取系统字典")
    public void delete(HttpServletResponse response, int[] id) {
        for (int _id : id) {
            dictionaryService.delete(_id);
        }
        writeInfo(response, JSON.toJSONString(new AjaxMsg()));
    }

    @RequestMapping("/query")
    @ResponseBody
    @SystemControllerLog(description = "获取系统字典列表")
    public JqgridPage query(HttpServletResponse response, int page, int rows, String sidx,
                            String sord, String filters) {
        PageHelper.startPage(page, rows);
        QueryFilters filters_ = null;
        if (filters == null || filters.trim().isEmpty()) {
            filters_ = new QueryFilters();
        } else {
            filters_ = JSON.parseObject(filters, QueryFilters.class);
        }
        filters_.setSidx(sidx);
        filters_.setSord(sord);
        List<Dictionary> list = dictionaryService.getByFilter(filters_);
        // 用PageInfo对结果进行包装
        PageInfo pageInfo = new PageInfo(list);
        return new JqgridPage(pageInfo.getTotal(), pageInfo.getPages(), pageInfo.getPageNum(),
                pageInfo.getList());
    }

    @RequestMapping("/getDic")
    @ResponseBody
    public List<Dictionary> getDic(String keyNo) {
        List<Dictionary> dics = dictionaryService.getDicByKeyNo(keyNo);
        return dics;
    }

}
